/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2007
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DNNContentBuilder.UI.WebControls
{
	/// <summary>
	/// This class is used at design time to select only valid data sources to page.
	/// </summary>
	/// <remarks>You should not used this class in your project.</remarks>
	public class PagedControlConverter : StringConverter
	{
		#region Implementation
		private object[] GetControls(IContainer container)
		{
			ArrayList controls = new ArrayList();
			
			foreach (IComponent component in container.Components)
			{
				Control control = component as Control;
				
				if (!(control is DataGrid) || control.ID == null || control.ID.Length == 0)
				{
					continue;
				}
				
				controls.Add(string.Copy(control.ID));
			}
			
			controls.Sort();
			return controls.ToArray();		
		}

		/// <summary>
		/// Returns a standard value collection.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <returns>A standard value collection.</returns>
		public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
		{
			object[] controls;

			if (context == null || context.Container == null)
			{
				return null;
			}
			
			controls = this.GetControls(context.Container);
			
			if (controls != null)
			{
				return new TypeConverter.StandardValuesCollection(controls);
			}
			
			return null;		
		}

		/// <summary>
		/// Get standard values.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <returns>A boolean value.</returns>
		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
		{
			return false;
		}

		/// <summary>
		/// Get standard values.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <returns>A boolean value.</returns>
		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
		{
			return true;
		}
		#endregion
	}
}
